package com.hnkywl.sts.dao.jhgl;


import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.jhgl.Jkd;
import com.hnkywl.sts.entity.zjgl.Pj;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-1
 * Time: 下午9:37
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class JkdDao extends SimpleHibernateTemplate<Jkd, Long> {
    /**
     * 查询电子借款单
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Jkd> findJkdList(Long jkgsIds, String jkgsMc, String gysMc, String jkdbm, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Jkd> page = new Page<Jkd>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (jkgsIds != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" jkgs.id = ? ");
            param.add(jkgsIds);
        }
        if (jkgsIds == null && jkgsMc != null && !jkgsMc.equals("")) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" jkgs.jc like ? ");
            param.add("%" + jkgsMc.trim() + "%");
        }

        if (gysMc != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" gys.jc   like  ?  ");
            param.add("%" + gysMc.trim() + "%");
        }
        if (jkdbm != null && !jkdbm.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" jkdbm like ? ");
            param.add("%" + jkdbm.trim() + "%");
        }
        //==============记录数量===============
        hql = "select count(*) from Jkd  where 1=1 " + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Jkd where 1=1 "+ RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        hql = hql + " order by dateCreated desc ";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public Page<Jkd> findytjjkdList(Long gysId, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Jkd> page = new Page<Jkd>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (gysId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" gys.id = ? ");
            param.add(gysId);
        }
        //==============记录数量===============
        hql = "select count(*) from Jkd where zt ='ysp' and (sfzfk is null or sfzfk <> 'zfk') and yfje < zfje ";
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Jkd  where zt = 'ysp' and (sfzfk is null or sfzfk <> 'zfk') and yfje < zfje ";
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        hql = hql + " order by dateCreated desc ";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }


    public List<Jkd> findJkdByZgzjjh(Long id) {
        String hql = "from Jkd where zgzjjh.id=?";
        return createQuery(hql, id).list();
    }

    public List<Jkd> findJkdByTpzjjh(Long id) {
        String hql = "from Jkd where tpzjjh.id=?";
        return createQuery(hql, id).list();
    }

    public Jkd findJkdByJkdBh(String jkdbh) {
        String hql = "from Jkd where jkdbm = ?";
        return (Jkd) createQuery(hql, jkdbh).list().get(0);
    }

    public List<Jkd> findJkdDetailByCght(Long cghtId) {
        String hql = "from Jkd where cght.id = ?";
        return createQuery(hql, cghtId).list();
    }

    /**
     * 首页电子借款单
     *
     * @return
     */
    public String findDzjkdSum(String zt) {
        String jkdSql = "select count(*) from T_BIZ_JKD where deleted = 0 and zt = ? ";
        return this.findBySQL(jkdSql,zt).get(0).toString();
    }
}
